//
//  HRZAccountPsdView.m
//  EMarket
//
//  Created by 石头 on 2018/5/8.
//  Copyright © 2018年 好日子. All rights reserved.
//

#import "HRZAccountPsdView.h"
#import "HRZUserInfo.h"


// Views
#import "UIView+Toast.h"
// Vendors
#import <SVProgressHUD.h>
#import "HRZGlobalNetManager.h"
#import "YDDevice.h"
#import "HRZSpeedy.h"

@interface HRZAccountPsdView()

/* 用户名 */
@property (weak, nonatomic) IBOutlet UITextField *userNameField;
/* 密码 */
@property (weak, nonatomic) IBOutlet UITextField *userPasswordField;
/* 登录 */
@property (weak, nonatomic) IBOutlet UIButton *loginButton;

@property (weak, nonatomic) IBOutlet UILabel *agreementLabel;


@end

@implementation HRZAccountPsdView

#pragma mark - Intial
- (void)awakeFromNib
{
    [super awakeFromNib];
    
    [self setUpBase];
}


#pragma mark - initialize
- (void)setUpBase
{
    _loginButton.enabled = NO;
    _loginButton.backgroundColor = [UIColor lightGrayColor];
    [_userNameField addTarget:self action:@selector(textFieldDidBeginEditing:) forControlEvents:UIControlEventEditingChanged];
    [_userPasswordField addTarget:self action:@selector(textFieldDidBeginEditing:) forControlEvents:UIControlEventEditingChanged];
    _userNameField.text = ([HRZObjManager hrz_readUserDataForKey:@"UserName"] == nil) ? nil : [HRZObjManager hrz_readUserDataForKey:@"UserName"];
    
    [HRZSpeedy hrz_setSomeOneChangeColor:_agreementLabel SetSelectArray:@[@"《",@"》",@"服",@"务",@"协",@"议"] SetChangeColor:RGB(56, 152, 181)];
    
}

- (IBAction)loginAccountClick:(UIButton *)sender {
    
    [self endEditing:YES];
    
    [SVProgressHUD show];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    
    WEAKSELF
    [[HRZGlobalNetManager getGlobalNetManager] onUserLoginByPhone:_userNameField.text passWord:_userPasswordField.text phoneIdentity:[YDDevice getUQID] complete:^(HRZUserInfo *userInfo, BOOL isSuc) {
        if (!isSuc) {
            
            [SVProgressHUD dismiss];
            
            [weakSelf makeToast:@"登陆失败" duration:0.6 position:CSToastPositionCenter];
            
            return;
        }
        
        [HRZObjManager hrz_saveUserData:@"1" forKey:@"isLogin"]; //1代表登录
        [HRZObjManager hrz_saveUserData:self.userNameField.text forKey:@"UserName"]; //记录用户名
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [SVProgressHUD dismiss];
            
            [weakSelf makeToast:@"登录成功" duration:0.6 position:CSToastPositionCenter];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.75 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                
                [[UIApplication sharedApplication].keyWindow.rootViewController dismissViewControllerAnimated:YES completion:^{
                    
                    [[NSNotificationCenter defaultCenter]postNotificationName:LOGINSELECTCENTERINDEX object:nil];
                }];
            });
        });

    }];
    
   
    
}

#pragma mark - <UITextFieldDelegate>
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (_userNameField.text.length != 0 && _userPasswordField.text.length != 0) {
        _loginButton.backgroundColor = RGB(252, 159, 149);
        _loginButton.enabled = YES;
    }else{
        _loginButton.backgroundColor = [UIColor lightGrayColor];
        _loginButton.enabled = NO;
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self endEditing:YES];
}

@end
